/*
 * PIMHelper.java
 *
 */
package OpenToday.PIMHelper;

import OpenToday.Tools.ExceptionUtils;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.pim.Event;
import javax.microedition.pim.EventList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
import javax.microedition.pim.ToDo;
import javax.microedition.pim.ToDoList;


/**
 * compared to other solutions there we query the PIM and let them decide if
 * the event is in the range.
 * usually we query on day. during the day we query current time to end of the day
 * @author thomas
 */
public final class PIMHelper {

    private static PIMHelper aPIMHelper;
    private final EventList m_phoneEvents;
    private final ToDoList m_phoneToDos;

    /** constructor is private, no instanciation from outside. */
    private PIMHelper() throws PIMException {

        m_phoneEvents = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_ONLY);
        m_phoneToDos  = (ToDoList)  PIM.getInstance().openPIMList(PIM.TODO_LIST,  PIM.READ_ONLY);
    }

    /** may uso of Lazy Evaluation without Synchronisisation-Overhead
    http://de.wikipedia.org/wiki/Singleton_(Entwurfsmuster)#Eager_Creation
     * @throws PIMException
     * @return we return one instance of this singelton class
     */
    public synchronized static PIMHelper getInstance() throws PIMException {
        if (aPIMHelper == null) {
            aPIMHelper = new PIMHelper();
        }
        return aPIMHelper;
    }

    /**
     * calculate all events (EventList) from time_start until
     * @throws Exception
     * @throws PIMException
     * @param time_start
     * @param time_end 
     * @return vector of PhoneCalendarEvent
     **/
    public Vector getPhoneCalendarEvents(long time_start, long time_end) throws PIMException, Exception {

        //calculate here if event belongst to this week
        //ExceptionUtils.PHASE("getPhoneCalendarEvents from " + time_start + " to " + time_end);
        Enumeration items = m_phoneEvents.items(EventList.OCCURRING, time_start, time_end, false);
        Vector events = new Vector();

        while (items.hasMoreElements()) {
            Event event = (Event) items.nextElement();

            if (null != event) {
                PhoneCalendarEvent phoneCalendarEvent =
                        new PhoneCalendarEvent(event);

                events.addElement((Object) phoneCalendarEvent);
            }
        }
        return events;
    }

    /**
     * query todo events from ToDoList
     * @param time_start
     * @param time_end
     * @return Vector of javax.microedition.pim.ToDo
     * @throws javax.microedition.pim.PIMException
     * @throws java.lang.Exception
     */
    public Vector getToDoEvents(long time_start, long time_end) throws PIMException, Exception {
        ExceptionUtils.PHASE("getToDoEvents from " + time_start + " to " + time_end);
        Enumeration todoItems = m_phoneToDos.items();
        Vector todoVect = new Vector();

        while(todoItems.hasMoreElements()){
            ToDo todo = (ToDo) todoItems.nextElement();

            if (null != todo) {
                PhoneCalendarToDo phoneToDoEvent = new PhoneCalendarToDo(todo);
                todoVect.addElement((Object) phoneToDoEvent);
            }
        }

        return todoVect;
    }




    /**
     * close PIM
     */
    public void Destroy() {
        try {
            m_phoneEvents.close();
            m_phoneToDos.close();
        } catch (PIMException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * determines if a field is available in array
     * @param fields
     * @param field
     * @return
     */
    static public boolean hasField(int[] fields, int field) {

        for (int i = 0; i < fields.length; i++) {
            int val = fields[i];
            if (val == field) {
                return true;
            }
        }
        return false;
    }

}